package com.example.myapp.database;

import android.database.Cursor;
import com.example.myapp.bill.Bill;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class BillsUtilities {
    public static List<Bill> getAllListaRachunkowFromCursorObiect(Cursor c) {
        List<Bill> listArray = new ArrayList<Bill>();
        c.moveToFirst();
        while (!c.isAfterLast()) {
            Bill listaRachunkow = createBillBy(c);
            listArray.add(listaRachunkow);
            c.moveToNext();
        }
        return listArray;
    }

    private static Bill createBillBy(Cursor c) {
        int billId = c.getInt(c.getColumnIndex("id_rachunku"));
        String billName = c.getString(c.getColumnIndex("nazwa"));
        String billData = c.getString(c.getColumnIndex("data"));
        long billAmount = c.getLong(c.getColumnIndex("kwota"));
        boolean ifPaid = Boolean.parseBoolean(c.getString(c.getColumnIndex("zaplacony")));

        return new Bill(billId, billName, billData, billAmount, ifPaid);
    }

    public static List<Bill> getAllListaRachunkowFromCursorObiectForMonth(Cursor c, String date) {
        List<Bill> listArray = new ArrayList<Bill>();
        c.moveToFirst();
        while (!c.isAfterLast()) {
            Bill rachunek = createBillBy(c);
            if (checkDate(date, rachunek.getDate()) && rachunek.getIfPaid())
                listArray.add(rachunek);
            c.moveToNext();
        }
        return listArray;
    }

    private static boolean checkDate(String date, String dateFromBill) {
        StringTokenizer tokenizer = new StringTokenizer(dateFromBill, "-");
        String billDay = String.valueOf(tokenizer.nextElement());
        String billMonth = String.valueOf(tokenizer.nextElement());
        String billYear = String.valueOf(tokenizer.nextElement());

        StringTokenizer tokenizer2 = new StringTokenizer(date, ".");
        String month = String.valueOf(tokenizer2.nextElement());
        String year = String.valueOf(tokenizer2.nextElement());

        return billMonth.equals(month) && billYear.equals(year);
    }
}